home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 194_01 / deff3.c < prev    next >
Text File  |  1987-04-14  |  6KB  |  410 lines

  1. /* [DEFF3.C of JUGPDS Vol.17]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *            Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited & tested by Y. Monma (JUG-C/M Disk Editor)       * 
  10. *                                *
  11. *****************************************************************
  12. */
  13.  
  14. /* Library functions for Software Tools */
  15.  
  16. #include "stdio.h"
  17.  
  18. #define    UBUFSIZE    64
  19.  
  20. getlin(s, lim)
  21. char    *s;
  22. int    lim;
  23. {
  24.     char    *p;
  25.     int    c;
  26.  
  27.     p = s;
  28.     while(--lim > 0 && (c = getchar()) != EOF && c != NEWLINE)
  29.         *s++ = c;
  30.     if(c == NEWLINE)
  31.         *s++ = c;
  32.     *s = '\0';
  33.     return(s-p);
  34. }
  35.  
  36.  
  37. getwrd (in, i, out)
  38. char in[], out[];
  39. int  *i;
  40. {
  41.     int    j;
  42.  
  43.     while (in[(*i)] == ' ' || in[(*i)] == '\t')
  44.             (*i)++;
  45.     j = 0;
  46.     while (in[(*i)] != '\0' && in[(*i)] != ' '
  47.         && in[(*i)] != '\t' && in[(*i)] != NEWLINE) {
  48.         out[j] = in[(*i)];
  49.             (*i)++;
  50.             j++;
  51.             }
  52.     out[j] = '\0';
  53.     return j;
  54. }
  55.  
  56.  
  57. getword(w, lim)
  58. char *w;
  59. int  lim;
  60. {
  61.     int c, t;
  62.  
  63.     if( type(*w++ = c = getchar()) != LETTER) {
  64.         *w = '\0';
  65.         return(c);
  66.     }
  67.     while(--lim > 0) {
  68.         t = type(*w++ = c = getchar());
  69.         if( t != LETTER && t != DIGIT ) {
  70.             ungetch(c);
  71.             break;
  72.         }
  73.     }
  74.     *(w-1) = '\0';
  75.     return(LETTER);
  76. }
  77.  
  78.  
  79. type(c)
  80. int c;
  81. {
  82.     if(isdigit(c)) return(DIGIT);
  83.     if(isalpha(c)) return(LETTER);
  84.     return(c);
  85. }
  86.  
  87.  
  88. fgetlin(fd, s, lim)
  89. char    *s;
  90. int    lim;
  91. {
  92.     char    *p;
  93.     int    c;
  94.  
  95.     p = s;
  96.     while(--lim > 0 && (c=getc(fd)) != EOF && c != CPMEOF && c != NEWLINE)
  97.         *s++ = c;
  98.     if(c == NEWLINE)
  99.         *s++ = c;
  100.     *s = '\0';
  101.     return(s-p);
  102. }
  103.  
  104. error(str)
  105. {
  106.     fprintf(STDERR,"%s\n", str);
  107.     exit();
  108. }
  109.  
  110.  
  111. char *itoa(s,n)
  112. char *s;
  113. {
  114.     char *p;
  115.  
  116.     p = s;
  117.     if (n < 0) {
  118.         *s++ = '-';
  119.         n = -n;
  120.         }
  121.     s = _itoa(s,n);
  122.     *s = '\0';
  123.     return p;
  124. }
  125.  
  126.  
  127. _itoa(s,n)
  128. char *s;
  129. {
  130.     int i;
  131.  
  132.     if ((i = n/10) != 0)
  133.         s = _itoa(s,i);
  134.     *s++ = n % 10 + '0';
  135.     return s;
  136. }
  137.  
  138.  
  139. quick(v, l, r, comp, exch)
  140. char    *v[];
  141. int    l, r;
  142. int    (*comp)(), (*exch)();
  143. {
  144.     int    vx, i, j;
  145.  
  146.     i = l;  j = r;
  147.     vx = v[ (l+r)/2 ];
  148.     while(i <= j) {
  149.         while( (*comp)(v[i], vx) < 0 )
  150.             i++;
  151.         while( (*comp)(vx, v[j]) < 0 )
  152.             j--;
  153.         if(i <= j) {
  154.             (*exch)(&v[j], &v[i]);
  155.             i++;
  156.             j--;
  157.             }
  158.         }
  159.     if(l < j) quick(v,l,j,comp,exch);
  160.     if(i < r) quick(v,i,r,comp,exch);
  161. }
  162.  
  163. atoi(n)
  164. char *n;
  165. {
  166.     int val, sign;
  167.     char c;
  168.  
  169.     val=0;    sign=1;
  170.     while (iswhite(c = *n)) ++n;
  171.     if (c== '-') {sign = -1; n++;}
  172.     while (isdigit(c = *n++)) val = val * 10 + c - '0';
  173.     return sign*val;
  174. }
  175.  
  176. char *strsave(s)
  177. char *s;
  178. {
  179.     char *p, *alloc();
  180.  
  181.     if ((p = alloc(strlen(s)+1)) != NULL)
  182.         strcpy(p, s);
  183.     return p;
  184. }
  185.  
  186.  
  187. char *strcat(s, t)
  188. char *s, *t;
  189. {
  190.     char *p;
  191.  
  192.     p = s;
  193.     while(*s)
  194.         s++;
  195.     strcpy(s, t);
  196.     return p;
  197. }
  198.  
  199.  
  200. int strlen(s)
  201. char *s;
  202. {
  203.     char *p;
  204.  
  205.     p = s;
  206.     while (*p) p++;
  207.     return(p - s);
  208. }
  209.  
  210.  
  211. char *strcpy(s, t)
  212. char *s, *t;
  213. {
  214.     char *p;
  215.  
  216.     p = s;
  217.     while (*s++ = *t++)
  218.         ;
  219.     return p;
  220. }
  221.  
  222.  
  223. int strcmp(s,t)
  224. char *s, *t;
  225. {
  226.     for ( ; *s == *t; s++, t++)
  227.         if (*s == '\0')
  228.             return 0;
  229.     return(*s - *t);
  230. }
  231.  
  232.  
  233. strdfcmp(s,t)
  234. char *s, *t;
  235. {
  236. int i;
  237.  
  238. i = 0;
  239.     do {
  240.         if (i > 0)
  241.           {s++; t++;}
  242.  
  243.         while( !(isalpha(*s) || isspace(*s) || isdigit(*s)) )
  244.             s++;
  245.         while( !(isalpha(*t) || isspace(*t) || isdigit(*t)) )
  246.             t++;
  247.         i++;
  248.         } while (tolower(*s) == tolower(*t) && *s != '\0');
  249.     return( tolower(*s) - tolower(*t) );
  250. }
  251.  
  252.  
  253. strdcmp(s,t)
  254. char *s, *t;
  255. {
  256. int i;
  257.  
  258. i = 0;
  259.     do {
  260.         if (i > 0)
  261.           {s++; t++;}
  262.  
  263.         while( !(isalpha(*s) || isspace(*s) || isdigit(*s)) )
  264.             s++;
  265.         while( !(isalpha(*t) || isspace(*t) || isdigit(*t)) )
  266.             t++;
  267.         i++;
  268.         } while (*s == *t && *s != '\0');
  269.     return( *s - *t);
  270. }
  271.  
  272.  
  273. strfcmp(s,t)
  274. char *s, *t;
  275. {
  276.     for ( ; tolower(*s) == tolower(*t); s++, t++)
  277.         if (*s == '\0')
  278.             return 0;
  279.     return(tolower(*s) - tolower(*t));
  280. }
  281.  
  282. poke(c,d)
  283. char *c, d;
  284. {
  285.     *c = d;
  286. }
  287.  
  288. /*----------------------------------------------------------*/
  289. /* Some functions from "The C programing language"
  290.                   by KERNIGHAN & RITCHIE */
  291. /*----------------------------------------------------------*/
  292.  
  293.  
  294. swap(px, py)
  295. int    *px, *py;
  296. {
  297.     int    temp;
  298.  
  299.     temp = *px;
  300.     *px = *py;
  301.     *py = temp;
  302. }
  303.  
  304. /* Can't compile by [BDS-C compiler]
  305.  
  306. fillbuf(fp)
  307. register FILE *fp;
  308. {
  309.     static char smallbuf[_NFILE];    /* for unbuffering input */
  310.     char *calloc();
  311.  
  312.     if ((fp->_flag & _READ) == 0 || (fp->_flag & (_EOF | _ERR)) != 0)
  313.         return(EOF);
  314.     while (fp->_base == NULL)
  315.         if (fp->_flag & _UNBUF)
  316.             fp->_base = &smallbuf[fp->_fd];
  317.         else if ((fp->_base = calloc(_BUFSIZE, 1)) == NULL)
  318.             fp->_flag |= _UNBUF;
  319.         else
  320.             fp->_flag |= _BIGBUF;
  321.     fp->_ptr = fp->_base;
  322.     fp->_cnt = read(fp->_fd, fp->_ptr, fp->_flag & _UNBUF ? 1 : _BUFSIZE);
  323.     if (--fp->_cnt < 0) {
  324.         if (fp->_cnt == -1)
  325.             fp->_flag |= _EOF;
  326.         else
  327.             fp->_flag |= _ERR;
  328.         fp->_cnt = 0;
  329.         return(EOF);
  330.     }
  331.     return(*fp->_ptr++ & CMASK);
  332. }
  333.  
  334.  
  335. flushbuf(c,fp)
  336. register FILE *fp;
  337. register char c;
  338. {
  339.     extern FILE _iob[];
  340.     int n;
  341.  
  342.     if (fp->_base == NULL)
  343.         return(EOF);
  344.     if ((fp->_flag & _WRITE) == 0 || (fp->_flag & (_EOF|_ERR)) != 0)
  345.         return(EOF);
  346.     fp->_ptr = fp->_base;
  347.     n = ( fp->_flag & _UNBUF ) ? 1 : _BUFSIZE;
  348.     if (write(fp->_fd, fp->_ptr, n) != n) {
  349.         fp->_flag |= _ERR;        
  350.         fprintf(stderr, "write error %d\n", fp->_fd);
  351.         exit(1);
  352.         }
  353.     fp->_ptr = fp->_base;
  354.     *fp->_ptr++ = c;
  355.     fp->_cnt = n-1;
  356.     return(c);
  357. }
  358.  
  359.  
  360. FILE *fopen(name, mode)
  361. register char *name, *mode;
  362. {
  363.     register int fd;
  364.     register FILE *fp;
  365.  
  366.  
  367.     if (*mode != 'r' && *mode != 'w' /* && *mode != 'a' */) {
  368.         fprintf(stderr, "illegal mode %s opening %s\n", mode, name);
  369.         exit(1);
  370.         }
  371.     for (fp = _iob; fp < _iob + _NFILE; fp++)
  372.          if ((fp->_flag & (_READ | _WRITE)) == 0)
  373.             break;
  374.     if (fp >= _iob + _NFILE)
  375.         return(NULL);
  376.     if (*mode == 'w')
  377.         fd = open(name, WRITE);
  378.     else
  379.         fd = open(name, READ);
  380.     if (fd = -1)
  381.         return(NULL);
  382.  
  383.     fp->_fd = fd;
  384.     fp->_cnt = 0;
  385.     fp->_base = NULL;
  386.     fp->_flag &= ~(_READ | _WRITE);
  387.     fp->_flag |= (*mode == 'r') ? _READ : _WRITE;
  388.     return(fp);
  389. }
  390.  
  391.  
  392. char *calloc(n, size)
  393. unsigned n, size;
  394. {
  395.     char *p, *cp;
  396.     char *alloc();
  397.     int i, j;
  398.  
  399.  
  400.     for (i = 0; i < n; i++) {
  401.         if ((p = alloc(size) ) == NULL)
  402.             return(NULL);
  403.         cp = p;
  404.         for (j = 0; j < size; j++)
  405.             *cp++ = 0;
  406.         }
  407.     return((char *)p );
  408. }
  409. */
  410.